#ifndef OMPL_GEOMETRIC_PLANNERS_RRT_HLAZY_RRT_H
#define OMPL_GEOMETRIC_PLANNERS_RRT_HLAZY_RRT_H

#include <ompl/geometric/planners/PlannerIncludes.h>
#include <ompl/datastructures/NearestNeighbors.h>
#include <vector>

namespace ompl
{

    namespace geometric
    {
        class hLazyRRT : public base::Planner
        {
        public:

            /** \brief Constructor */
            hLazyRRT(const base::SpaceInformationPtr &si) : base::Planner(si, "hLazyRRT")
            {
                goalBias_ = 0.05;
                maxDistance_ = 0.0;
            }

            virtual ~hLazyRRT(void)
            {
                freeMemory();
            }

            virtual void getPlannerData(base::PlannerData &data) const;

            virtual bool solve(const base::PlannerTerminationCondition &ptc);

            virtual void clear(void);

            /** \brief Set the goal biasing.

                In the process of randomly selecting states in the state
                space to attempt to go towards, the algorithm may in fact
                choose the actual goal state, if it knows it, with some
                probability. This probability is a real number between 0.0
                and 1.0; its value should usually be around 0.05 and
                should not be too large. It is probably a good idea to use
                the default value. */
            void setGoalBias(double goalBias)
            {
                goalBias_ = goalBias;
            }

            /** \brief Get the goal bias the planner is using */
            double getGoalBias(void) const
            {
                return goalBias_;
            }

            /** \brief Set the range the planner is supposed to use.

                This parameter greatly influences the runtime of the
                algorithm. It represents the maximum length of a
                motion to be added in the tree of motions. */
            void setRange(double distance)
            {
                maxDistance_ = distance;
            }

            /** \brief Get the range the planner is using */
            double getRange(void) const
            {
                return maxDistance_;
            }

            /** \brief Set a different nearest neighbors datastructure */
            template<template<typename T> class NN>
            void setNearestNeighbors(void)
            {
                nn_.reset(new NN<Motion*>());
            }

            virtual void setup(void);

        protected:

            /** \brief Representation of a motion */
            class Motion
            {
            public:

                Motion(void) : state(NULL), parent(NULL), valid(false)
                {
                }

                /** \brief Constructor that allocates memory for the state */
                Motion(const base::SpaceInformationPtr &si) : state(si->allocState()), parent(NULL), valid(false)
                {
                }

                ~Motion(void)
                {
                }

                /** \brief The state contained by the motion */
                base::State          *state;

                /** \brief The parent motion in the exploration tree */
                Motion               *parent;

                /** \brief Flag indicating whether this motion has been validated */
                bool                  valid;

                /** \brief The set of motions that descend from this one */
                std::vector<Motion*>  children;
            };

            /** \brief Free the memory allocated by this planner */
            void freeMemory(void);

            /** \brief Remove a motion from the tree datastructure */
            void removeMotion(Motion *motion);

            /** \brief Compute distance between motions (actually distance between contained states) */
            double distanceFunction(const Motion* a, const Motion* b) const
            {
                return si_->distance(a->state, b->state);
            }

            /** \brief State sampler */
            base::StateSamplerPtr                          sampler_;

            /** \brief A nearest-neighbors datastructure containing the tree of motions */
            boost::shared_ptr< NearestNeighbors<Motion*> > nn_;

            /** \brief The fraction of time the goal is picked as the state to expand towards (if such a state is available) */
            double                                         goalBias_;

            /** \brief The maximum length of a motion to be added to a tree */
            double                                         maxDistance_;

            /** \brief The random number generator */
            RNG                                            rng_;

        };

    }
}

#endif
